﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace ZeroTouchUI
{
    /// <summary>
    /// Interaction logic for AddNewAction.xaml
    /// </summary>
    public partial class AddNewAction : Window
    {
        ComboBox commandlist;
        Profile profile;
        KeyValuePair<Gesture, Action> newPair;

        public AddNewAction(ComboBox cl, KeyValuePair<Gesture, Action> np, Profile pro)
        {
            InitializeComponent();
            commandlist = cl;
            profile = pro;
            newPair = np;
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {

        }

        private void page1_radiobutton_Checked(object sender, RoutedEventArgs e)
        {
            p1_1_label.Visibility = System.Windows.Visibility.Visible;
            destination_textbox.Visibility = System.Windows.Visibility.Visible;
            example_label.Visibility = System.Windows.Visibility.Visible;

            p2_1_label.Visibility = System.Windows.Visibility.Hidden;
            targetprogram_textbox.Visibility = System.Windows.Visibility.Hidden;
            browseButton.Visibility = System.Windows.Visibility.Hidden;
            p2_2_label.Visibility = System.Windows.Visibility.Hidden;
            key2send_textbox.Visibility = System.Windows.Visibility.Hidden;
            label1.Visibility = System.Windows.Visibility.Hidden;
            label2.Visibility = System.Windows.Visibility.Hidden;
            label3.Visibility = System.Windows.Visibility.Hidden;
            label4.Visibility = System.Windows.Visibility.Hidden;

        }

        private void page2_radiobutton_Checked(object sender, RoutedEventArgs e)
        {
            p1_1_label.Visibility = System.Windows.Visibility.Hidden;
            destination_textbox.Visibility = System.Windows.Visibility.Hidden;
            example_label.Visibility = System.Windows.Visibility.Hidden;

            p2_1_label.Visibility = System.Windows.Visibility.Visible;
            targetprogram_textbox.Visibility = System.Windows.Visibility.Visible;
            browseButton.Visibility = System.Windows.Visibility.Visible;
            p2_2_label.Visibility = System.Windows.Visibility.Visible;
            key2send_textbox.Visibility = System.Windows.Visibility.Visible;
            label1.Visibility = System.Windows.Visibility.Visible;
            label2.Visibility = System.Windows.Visibility.Visible;
            label3.Visibility = System.Windows.Visibility.Visible;
            label4.Visibility = System.Windows.Visibility.Visible;
        }

        private void addaction_button_Click(object sender, RoutedEventArgs e)
        {
            if (page1_radiobutton.IsChecked == true)
            {
                if (name_textbox.Text != "" && destination_textbox.Text != "")//check for valid input
                {
                    if (profile.getDictionary().Any(g => g.Value.Name.ToLower() == name_textbox.Text.ToLower()))
                    {
                        NameTaken nt = new NameTaken();
                        nt.ShowDialog();
                    }
                    else
                    {
                        // add this event to the combo box
                        ComboBoxItem temp = new ComboBoxItem();
                        temp.Content = name_textbox.Text;
                        commandlist.Items.Add(temp);
                        // now add it to the profile
                        Action a = new Action();
                        a.Name = name_textbox.Text;
                        a.Info = destination_textbox.Text;
                        a.CustomData = "1";
                        a.Type = "1";
                        profile.AddAction(a);

                        //newPair.Value.Info = destination_textbox.Text;

                        this.Close();
                    }
                }
                else
                {
                    // "Please add a webpage or program name in the provided textbox"
                }
            }
            else if (page2_radiobutton.IsChecked == true)
            {
                if (name_textbox.Text != "" && targetprogram_textbox.Text != "" && key2send_textbox.Text != "")
                {
                    if (profile.getDictionary().Any(g => g.Value.Name.ToLower() == name_textbox.Text.ToLower()))
                    {
                        NameTaken nt = new NameTaken();
                        nt.ShowDialog();
                    }
                    else
                    {
                        // add this event to the combo box
                        ComboBoxItem temp = new ComboBoxItem();
                        temp.Content = name_textbox.Text;
                        commandlist.Items.Add(temp);
                        // now add it to the profile
                        Action a = new Action();
                        a.Name = name_textbox.Text;
                        a.Info = targetprogram_textbox.Text;
                        a.CustomData = key2send_textbox.Text;
                        a.Type = "2";
                        profile.AddAction(a);

                        /*newPair.Value.Info = targetprogram_textbox.Text;
                        newPair.Value.CustomData = key2send_textbox.Text;*/

                        this.Close();
                    }
                }
                else
                {
                    // "Please fill out ALL of the forms provided"
                }
            }
            else
            {
                // "Please select a type of event to add
            }
        }

        private void cancel_button_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void browseButton_Click(object sender, RoutedEventArgs e)
        {
            Microsoft.Win32.OpenFileDialog open = new Microsoft.Win32.OpenFileDialog();
            open.DefaultExt = ".exe";

            if (open.ShowDialog() == true && System.IO.Path.GetExtension(open.FileName) == ".exe")
            {
                targetprogram_textbox.Text = System.IO.Path.GetFileNameWithoutExtension(open.FileName);
            }
            else
            {
                if (open.FileName != String.Empty)
                {
                    System.Windows.MessageBox.Show("Please select an executable application file.\n\n Example: \"notepad.exe\"",
                                                   "Incorrect File Type", MessageBoxButton.OK, MessageBoxImage.Information);
                }
            }
        }
    }
}
